// 人脸识别视图

//var $pic1, $pic2, $pic3;
var $verifyBtn, $phone, $verification_code, $name, $frId, $agreeInput;
var daojs = 60;
var stepNum = 0;
var stepTimer;
var $steps;
var idRegExp;
var tempName, tempId, temPhone, tempCode, tempVerifyBtn;
/*
import{
    idRegExp
} from '../components/commonRegExp.js';*/
import {
    titileInputInit
} from '../components/titleInput.js';

// 人脸识别 值对应码
var errorCodeMessage = {
    "-4006": "视频中自拍照特征提取失败",
    "-4007": "视频中自拍照之间对比失败",
    "-4009": "Card照片提取特征失败",
    "-4010": "自拍照与Card照片相似度计算失败",
    "-4015": "自拍照人脸检测失败",
    "-4016": "自拍照解码失败",
    "-4017": "Card照片人脸检测失败",
    "-5001": "Card照片解码失败",
    "-5002": "唇语失败",
    "-5005": "自拍照解析照片不足",
    "-5007": "视频没有声音",
    "-5008": "声音识别失败",
    "-5009": "视频人脸检测失败，没有嘴或者脸",
    "-5010": "唇动失败",
    "-5011": "活体检测失败--不是活体",
    "-5012": "视频中噪声太大",
    "-5013": "视频里的声音太小",
    "-5014": "活体检测level参数无效",
    "-5015": "视频像素太低，最小320*480",
    "-5016": "视频里的人不是活体（疑似翻拍视频等攻击）",
    "-5801": "请求缺少身份证号码或身份证姓名",
    "-5802": "服务器内部错误，服务暂时不可用，请稍后重试",
    "-5803": "身份证姓名与身份证号码不一致",
    "-5804": "身份证号码无效",
    "-5805": "用户未输入图像或者 url 下载失败",
    "-5806": "身份证号码或者身份证姓名格式错误",
    "-5807": "查询身份证信息失败",
    "-9999": "视频验证人与身份证本人相似度低于50%"

};
var scoreMap = [{
    "id": 5,
    "value1": "800",
    "valueType": "信用分",
    "value2": "1000",
    "valueName": "极好",
    "message": "[800,1000)"
}, {
    "id": 4,
    "value1": "700",
    "valueType": "信用分",
    "value2": "800",
    "valueName": "优秀",
    "message": "[700,800)"
}, {
    "id": 3,
    "value1": "600",
    "valueType": "信用分",
    "value2": "700",
    "valueName": "良好",
    "message": "[600,700)"
}, {
    "id": 2,
    "value1": "500",
    "valueType": "信用分",
    "value2": "600",
    "valueName": "一般",
    "message": "[500,600)"
}, {
    "id": 1,
    "value1": "0",
    "valueType": "信用分",
    "value2": "500",
    "valueName": "较差",
    "message": "[0,500)"
}];

function getscoreMap() {
    $.post(scoreIndexServlet, {}, function(sdata) {
        if (sdata.code == "0000" && sdata.data && sdata.data != null && sdata.data != "null" && sdata.data != "undefined" && sdata.data.length > 0) {
            scoreMap = sdata.data;
        }
    });
}

function viewInit(argument) {
    initVal();
    infoInit();

    titileInputInit();
    bindViewEvent();
    $("#fanhui").on("touchstart", function() {
        window.history.back();
    });

    getscoreMap();

}

// 清空填的信息
function emptyInfo() {
    $("#name").val("");
    $("#frId").val("");
    $("#phone").val("");
    $("#bankNo").val("");
    $("#submit_btn").attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
}

// 验证输入框 是否允许提交
function checkedInputValue() {
    var nameVal = $name.val();
    var frIdVal = $frId.val();
    var nameLength = len(nameVal);
    var isCard = true;
    var carIdValue = $("#bankNo").val().replace(/\D/g, "");
    if (carIdValue.length < 13 || carIdValue.length > 19) {
        isCard = false;
    }
    var phoneVal = $phone.val().replace(/\D/g, "");
    if (phoneVal.length == 11 && isCard && nameVal.replace(/[^\u4e00-\u9fa5]/g, "").length > 1 && nameVal.length == nameVal.replace(/[^\u4e00-\u9fa5]/g, "").length && idRegExp.test(frIdVal) && $agreeInput.attr("checked")) {
        $("#submit_btn").attr("class", "weui-btn weui-btn_primary");
    } else {
        $("#submit_btn").attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}

// 计算姓名长度
function getNameLength() {
    var nameVal = $name.val();
    var nameLength = len(nameVal);
    return nameLength;
}

//取字符串长度，中文算2位
function len(s) {
    var l = 0;
    var a = s.split("");
    for (var i = 0; i < a.length; i++) {
        if (a[i].charCodeAt(0) < 299) {
            l++;
        } else {
            l += 2;
        }
    }
    return l;
}

//
function idInput() {
    var idVal = $frId.val();
    idVal = idVal.replace(/\D/g, "X");
    $frId.val(idVal);
}

//

// 绑定事件
function bindViewEvent() {
    $("#phone").on("input", function() {
        var oldPhone = $(this).val();

    });

    $(".weui-cells_form").on("input", checkedInputValue);
    $("#phone").on("change", function() {
        var phoneVal = $(this).val().replace(/\D/g, "");
        if (phoneVal.length == 11) {
            $("#phone_panel").find(".weui-label").text("手机号").removeClass("info-prompt");
        } else {

            $("#phone_panel").find(".weui-label").text("手机号错误").addClass("info-prompt");
        }
    });
    $("#frId").on("change", function() {
        idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
        var frIdVal = $frId.val();
        if (idRegExp.test(frIdVal)) {
            $("#frId_panel").find(".weui-label").text("身份证号").removeClass("info-prompt");
        } else {
            $("#frId_panel").find(".weui-label").text("身份证号错误").addClass("info-prompt");
        }

    });
    $("#name").on("change", function() {
        var nameVal = $name.val();
        var nameLength = len(nameVal);
        if (nameVal.replace(/[^\u4e00-\u9fa5]/g, "").length > 1 && nameVal.length == nameVal.replace(/[^\u4e00-\u9fa5]/g, "").length) {
            $("#name_panel").find(".weui-label").text("姓名").removeClass("info-prompt");
        } else {
            $("#name_panel").find(".weui-label").text("姓名错误").addClass("info-prompt");
        }

    });
    $("#bankNo").on("change", function() {
        var carIdValue = $("#bankNo").val().replace(/\D/g, "");
        if (carIdValue.length > 12 && carIdValue.length < 20) {
            $("#bankNo_panel").find(".weui-label").text("银行卡号").removeClass("info-prompt");
        } else {
            $("#bankNo_panel").find(".weui-label").text("银行卡号错误").addClass("info-prompt");
        }

    });
    $frId.on("input", idInput);
    $("#checkButton").on("click", function() {
        if ($agreeInput.attr("checked")) {
            $(".icheckbox-blue").removeClass("checked");
            $agreeInput.attr("checked", "");
            $agreeInput.removeAttr("checked");

        } else {
            $(".icheckbox-blue").addClass("checked");
            $agreeInput.attr("checked", "");
        }
        checkedInputValue();

    });
    $("#contract").on("click", function() {
        tempInfo();
        location.href = "./contract.html";
    });
}



function initVal() {
    $steps = $(".step-box").children();
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $name = $("#name");
    $agreeInput = $("#checkButton").find("input");
    $frId = $("#frId");

    idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;

}

/*duhui 初始化第二步 */

function initStep2View() {
    stepTimer = setInterval(moveNext, 1500);
}
/*步骤显示*/
function moveNext() {
    $steps.eq(stepNum).fadeIn();
    stepNum++;
    if (stepNum > 4) {

        $('#recording').fadeIn();
        $('#recording').css({
            "display": "block"
        });
        clearInterval(stepTimer);
    }
}



// 设置验证结算
function setStepLoaderEnd(stepVal) {
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    var selector_duiguo = "#step3 .step_" + stepVal + " .duiguo_parent";
    $(selector_loader).hide();
    $(selector_duiguo).show();
}
// 正在验证查询第几步骤
function setStepLoader(stepVal) {
    var selector_wait = "#step3 .step_" + stepVal + " .step3_wait";
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    $(selector_wait).hide();
    $(selector_loader).show();
}

// 验证步骤还原到待验证
function initStepLoader() {
    $("#step3 .loader-inner.ball-beat").hide();
    $("#step3 .step3_wait").show();
    $("#step3 .duiguo_parent").hide();
}

//临时存储输入框信息　便于用户返回时不用重新填
function tempInfo() {
    sessionStorage.setItem("tempName", $("#name").val());
    sessionStorage.setItem("tempId", $("#frId").val());
    sessionStorage.setItem("temPhone", $("#phone").val());
    sessionStorage.setItem("tempCode", $("#verification_code").val());


}
//进入页面初始化
function infoInit() {
    if (sessionStorage.getItem("tempName") != "undefind") {
        setTempInfo();
        if ($("#phone").val().length == 11) {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        } else {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
        checkedInputValue();
        deleteTempInfo();
    }


}
//设值输入框的值
function setTempInfo() {
    tempName = sessionStorage.getItem("tempName");
    $("#name").val(tempName);
    tempId = sessionStorage.getItem("tempId");
    $("#frId").val(tempId);
    temPhone = sessionStorage.getItem("temPhone");
    $("#phone").val(temPhone);
    tempCode = sessionStorage.getItem("tempCode");
    $("#verification_code").val(tempCode);

}
//删除存储中的数据
function deleteTempInfo() {
    sessionStorage.removeItem("tempName");
    sessionStorage.removeItem("tempId");
    sessionStorage.removeItem("temPhone");
    sessionStorage.removeItem("tempCode");
    sessionStorage.removeItem("tempVerifyBtn");

}

// 判断是否为数组
function isArray(o) {
    return Object.prototype.toString.call(o) == '[object Array]';
}

// 数据处理
function processingData(queryData, $tparent) {
    var dangerClass = {
        index: -1,
        className: ""
    };
    var createTabelData, createTableTitle;
    var trData;
    if (queryData.code != "0000") {
        window.step = 0;
        dangerClass.className = "label-danger";
        //        $localLoading.hide();
    }
    if (!queryData.interId || queryData.interId == null || queryData.interId == "null" || queryData.interId == "undefind") {
        createTabelData = [{
            "title": "当前接口",
            "value": "验证失败"
        }];
        createTableTitle = "接口验证";
    }
    if (queryData.funcId == "008") { // 商务信用分
        if (queryData.code == "0000") {
            if (queryData.verify && queryData.verify != "" && queryData.verify != "null" && queryData.verify != null && queryData.verify != "undefined") {
                var verify = JSON.parse(queryData.verify);
                var xypf = verify[verify.length - 1][1];
                var xypj = "";
                for (var i = 0, l = scoreMap.length; i < l; i++) {
                    if (parseInt(xypf) >= parseInt(scoreMap[i].value1) && parseInt(xypf) < parseInt(scoreMap[i].value2)) {
                        xypj = scoreMap[i].valueName;
                    }
                }
                var xyfpj = xypf + " (" + xypj + ")";
                createTabelData = [{
                    "title": "信用分",
                    "value": xyfpj
                }];
            } else {
                createTabelData = [{
                    "title": "信用分",
                    "value": ""
                }];
            }
        } else {
            createTabelData = [{
                "title": "信用分",
                "value": "计算失败"
            }];
        }
        createTableTitle = "信用分计算";
    } else if (queryData.interId == "0002" && queryData.channelId == "tengxun") { // 反欺诈
        if (queryData.code == "0000") {
            if (queryData.verify && queryData.verify != "" && queryData.verify != "null" && queryData.verify != null && queryData.verify != "undefined") {
                var fqzf = queryData.verify;
                createTabelData = [{
                    "title": "社交反欺诈分",
                    "value": fqzf
                }];
            }
        } else {
            // "验证失败";
        }

        createTableTitle = "社交反欺诈分计算"; // identifyMap[0].name;

    } else if (queryData.interId == "M005" && queryData.channelId == "suanhua") { // 手机号实名制比对结果
        if (queryData.code == "0000") {
            if (queryData.verify == "2") {
                //"比对不一致";
                createTabelData = [{
                    "title": "基本信息",
                    "value": "比对不一致"
                }];
            } else if (queryData.verify == "1") {
                //"比对一致";
                createTabelData = [{
                    "title": "基本信息",
                    "value": "比对一致"
                }];
            }
        } else {
            // "验证失败";
        }

        createTableTitle = "基本信息比对"; // identifyMap[0].name;

    } else if (queryData.interId == "0001" && queryData.channelId == "kexin") { // 不良信息黑黑名单
        if (queryData.code == "0000") {
            if (queryData.data != "" && queryData.data != null && queryData.data != "null" && queryData.data != "undefind") {
                //    identifyMap[2].data[3].value = queryData.data;
                createTabelData = queryData.data;
            } else {
                createTabelData = [{
                    "title": "不良信息(刑事、民事)",
                    "value": "没有不良信息"
                }];
                //                identifyMap[2].data[3].value = "没有不良信息";
            }
        } else {
            dangerClass.index = 0;
            createTabelData = [{
                "title": "不良信息(刑事、民事)",
                "value": "验证失败"
            }];
            //identifyMap[2].data[3].value = "验证失败";
        }
        //       createTabelData = identifyMap[2].data;
        createTableTitle = "不良信息(刑事、民事)";

    } else if (queryData.interId == "0001" && queryData.channelId == "tengxun") {
        // 优图视频人脸识别
        var vfMsg = "人脸识别失败";
        if (queryData.code == "0000") {
            if (queryData.verify == "0") {
                if (parseInt(queryData.verifyMsg) > 50 && parseInt(queryData.verifyMsg) < 101) {
                    vfMsg = "验证通过(相似度" + queryData.verifyMsg + ")";
                } else {
                    vfMsg = "非身份证本人(相似度" + queryData.verifyMsg + ")";
                }
            }else{
                vfMsg = "人脸识别失败";
                if(queryData.verify == "-0001") {

                    var codeArray = queryData.verifyMsg.split(",");
                    if (codeArray[0] != "0") {
                        vfMsg = errorCodeMessage[codeArray[0]];
                    } else if (codeArray[1]) {
                        vfMsg = errorCodeMessage[codeArray[1]];
                    }
                }else {
                    if(errorCodeMessage[queryData.verify]){
                        vfMsg = errorCodeMessage[queryData.verify];
                    }
                }
            }
        }
        createTabelData = [{
            "title": "人脸比对",
            "value": vfMsg
        }];
        createTableTitle = "活体人脸识别";
    } else if (queryData.interId == "8002" && queryData.channelId == "suanhua") { // 手机黑名单
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                if (queryData.data && queryData.data != "") {
                    createTabelData = queryData.data;
                } else {
                    createTabelData = [{
                        "title": "手机号黑名单",
                        "value": "没有数据"
                    }];
                }
                //        identifyMap[3].data[3].value = "查得";
            } else {
                createTabelData = [{
                    "title": "手机号黑名单",
                    "value": "未查得"
                }];
                //identifyMap[3].data[3].value = "未查得";
            }
        } else {
            dangerClass.index = 1;
            createTabelData = [{
                "title": "手机号黑名单",
                "value": "验证失败"
            }];
            //    identifyMap[3].data[3].value = "验证失败";
        }
        //        createTabelData = identifyMap[3].data;
        createTableTitle = "手机号黑名单";

    } else if (queryData.interId == "3012" && queryData.channelId == "suanhua") { // 手机信用查找

        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //                identifyMap[5].data[3].value = "查得";
                if (queryData.data && queryData.data != "" && queryData.data != null && queryData.data != "undefind") {
                    createTabelData = queryData.data;
                } else {
                    createTabelData = [{
                        "title": "手机号信用核查",
                        "value": "没有数据"
                    }];
                }
            } else if (queryData.verify == "2") {
                //                identifyMap[5].data[3].value = "未查得";
                createTabelData = [{
                    "title": "手机号信用核查",
                    "value": "未查得"
                }];
            } else if (queryData.verify == "31") {
                //                identifyMap[5].data[3].value = "请求参数错误";
                createTabelData = [{
                    "title": "手机号信用核查",
                    "value": "请求参数错误"
                }];
            } else if (queryData.verify == "5") {
                //        identifyMap[5].data[3].value = "电话卡不支持";
                createTabelData = [{
                    "title": "手机号信用核查",
                    "value": "请求参数错误"
                }];
            } else {
                //        identifyMap[5].data[3].value = "未查得";
                createTabelData = [{
                    "title": "手机号信用核查",
                    "value": "未查得"
                }];
            }
        } else {
            dangerClass.index = 1;
            //    identifyMap[5].data[3].value = "验证失败";
            createTabelData = [{
                "title": "手机号信用核查",
                "value": "验证失败"
            }];
        }
        //        createTabelData = identifyMap[5].data;
        createTableTitle = "手机号信用核查";
    } else if (queryData.interId == "1003" && queryData.channelId == "suanhua") { // 手机信用查找
        if (queryData.code == "0000") {

            if (queryData.verify == "1") {
                //identifyMap[1].data[3].value = "比对一致";
                createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": "比对一致"
                }];
            } else if (queryData.verify == "2") {
                //identifyMap[1].data[3].value = "比对不一致";
                createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": "比对不一致"
                }];
            } else if (queryData.verify == "31") {
                // identifyMap[1].data[3].value = "请求参数错误";
                createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": "请求参数错误"
                }];
            } else if (queryData.verify == "5") {
                // identifyMap[1].data[3].value = "银行卡不支持";
                createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": "银行卡不支持"
                }];
            } else {
                //         identifyMap[1].data[3].value = "未查得";
                createTabelData = [{
                    "title": "姓名、身份证、银行卡",
                    "value": "未查得"
                }];
            }
        } else {
            dangerClass.index = 1;
            createTabelData = [{
                "title": "姓名、身份证、银行卡",
                "value": "比对失败"
            }];
            //      identifyMap[1].data[3].value = "验证失败";
        }
        //        createTabelData = identifyMap[1].data;
        createTableTitle = "银行卡信息验证";
    } else if (queryData.interId == "2001" && queryData.channelId == "suanhua") { // 算话多重身份
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //"查得";
            } else {
                //"未查得";
            }
        } else {
            //"验证失败";
        }

        createTableTitle = "";
    } else if (queryData.interId == "8005" && queryData.channelId == "suanhua") { // 算话逾期信息
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //"查得";
            } else {
                //"未查得";
            }
        } else {
            // "验证失败";
        }
        createTableTitle = "";
    } else if (queryData.interId == "8006" && queryData.channelId == "suanhua") { // 算话贷款信息
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //"查得";
            } else {
                //"未查得";
            }
        } else {
            //"验证失败";
        }
        createTableTitle = "";
    } else if (queryData.interId == "5003" && queryData.channelId == "suanhua") { // 算话贷款信息
        if (queryData.code == "0000") {
            if (queryData.verify == "1") {
                //"查得";
            } else {
                //"未查得";
            }
        } else {
            //"验证失败";
        }
        createTableTitle = "";
    } else {
        createTabelData = [{
            "title": "当前接口",
            "value": "验证失败"
        }];
        createTableTitle = "接口验证";
    }

    /*  if (queryData.next == 0) {
          $localLoading.hide();
      }*/
    /*    if (step == 0) {*/
    addResultCells(createTabelData, createTableTitle, dangerClass);
    /*    } else {
            insertTr(trData, $identifyResultBox, dangerClass);
        }*/

}


// 拼接验证结果html
function addResultCells(cellsData, title, danger) {
    var resultHtml = `<div class="weui-cells__title">${title}</div><div class="weui-cells">`;
    for (var i = 0, l = cellsData.length; i < l; i++) {
        resultHtml += `
<div class="weui-cell">
    <div class="weui-cell__hd">${cellsData[i].title}</div>
    <div class="weui-cell__bd">`;
        if (isArray(cellsData[i].value)) {
            for (var n = 0; n < cellsData[i].value.length; n++) {
                resultHtml += `<p>${cellsData[i].value[i]}</p>`;
            }
        } else {
            resultHtml += `<p>${cellsData[i].value}</p>`;
        }
        resultHtml += `</div></div>`;
    }
    resultHtml += '</div></div>';

    $("#validate-result").append(resultHtml);
}

export {
    viewInit,
    /*setDemoPic,
    setRoundLeft,*/
    setStepLoader,
    setStepLoaderEnd,
    initStepLoader,
    initStep2View,
    emptyInfo,
    processingData,
    addResultCells
}
